#include <stdio.h>

// int main(){
//     printf("hello world");
//     return 0;
// }

#include <windows.h>
#include <tchar.h>

#include <windows.h>
#include <tchar.h>

// 定义窗口过程回调函数
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        case WM_CLOSE:
            PostQuitMessage(0);
            return 0;
        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    // 注册窗口类
    const TCHAR CLASS_NAME[] = _T("Sample Window Class");
    WNDCLASS wc = {};

    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;
    RegisterClass(&wc);

    // 从注册表读取键值
    HKEY hKey;
    TCHAR buffer[MAX_PATH] = { 0 };
    DWORD bufLen = sizeof(buffer);
    DWORD dwType = REG_SZ;

    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion"), 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
        _tcscpy_s(buffer, _T("Failed to open key."));
    } else {
        if (RegQueryValueEx(hKey, _T("ProgramFilesDir"), NULL, &dwType, (LPBYTE)buffer, &bufLen) != ERROR_SUCCESS) {
            _tcscpy_s(buffer, _T("Failed to read key value."));
        }
        RegCloseKey(hKey);
    }

    // 创建窗口并显示读取的注册表键值
    HWND hwnd = CreateWindowEx(0, CLASS_NAME, _T("Read Registry Key Value"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hInstance, NULL);
    if (hwnd == NULL) {
        return 0;
    }

    CreateWindow(_T("STATIC"), buffer, WS_CHILD | WS_VISIBLE, 10, 10, 450, 25, hwnd, NULL, hInstance, NULL);

    ShowWindow(hwnd, nCmdShow);

    MSG msg = {};
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}
